package com.bop.dehong.base;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.base.dehong.util.LogUtils;
import com.base.dehong.util.NetWorkUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.config.Config;
import com.bop.dehong.webview.JavaScriptObject;

/**
 * 继承此类，布局中必须要有WebView，且ID为webview1
 * @author TangWenhan/YeFengyun
 *2015-11-16下午2:54:58
 */
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
public abstract class WebActivityBaseFrame extends BaseActivity{
	private WebView mWebView;

	@Override
	protected void initUI(){
		mWebView = getView(R.id.webview1);
		/**
		 * 打开网页时不调用系统浏览器， 而是在本WebView中显示
		 */
		mWebView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				ToastUtils.show(WebActivityBaseFrame.this, "网络异常，请稍后再试");
				mWebView.loadUrl("about:blank");
				disappearProgressDialog();
				super.onReceivedError(view, errorCode, description, failingUrl);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				disappearProgressDialog();
				if (url.endsWith(Config.URL_HuifuSucc)) {
					setResult(RESULT_OK);
					finishCurrentActivity();
				}else if (url.endsWith(Config.URL_HuifuFail)) {
					ToastUtils.show(WebActivityBaseFrame.this, "当前操作失败，请重试！");
					finishCurrentActivity();
				}else if (url.endsWith(Config.URL_HuifuSucc4AddCard)) {
					setResult(RESULT_OK);
//					finishCurrentActivity();
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				LogUtils.e("WebView:url"+url);
				showProgressDialog();
			}
			
		});
		WebSettings webSettings = mWebView .getSettings();       
		webSettings.setJavaScriptEnabled(true); 
		webSettings.setSupportZoom(true);
		//打开页面时， 自适应屏幕：
		webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setRenderPriority(RenderPriority.HIGH);  
		if (NetWorkUtils.isConnect(this)) {// 有网络
			webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式  
		}else {
			webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		}
        // 开启 DOM storage API 功能  
		webSettings.setDomStorageEnabled(true);  
        //开启 database storage API 功能  
		webSettings.setDatabaseEnabled(true);   
        String cacheDirPath = this.getCacheDir().getAbsolutePath();  
  //      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;  
        LogUtils.e("cachepath", "cacheDirPath=" + cacheDirPath);  
        //设置数据库缓存路径  
        webSettings.setDatabasePath(cacheDirPath);  
        //设置  Application Caches 缓存目录  
        webSettings.setAppCachePath(cacheDirPath);  
        //开启 Application Caches 功能  
        webSettings.setAppCacheEnabled(true);
		mWebView.addJavascriptInterface(new JavaScriptObject(this),"android");


	}
	public void loadUrl(String url){
		mWebView.loadUrl(url);
	}
	public abstract void setTitle(String title);
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {       
		if ((keyCode == KeyEvent.KEYCODE_BACK) &&   mWebView .canGoBack()) {       
			mWebView.goBack();       
			return true;       
		}       
		return super.onKeyDown(keyCode, event);       
	}
}

